/*
 * @Description: 档案鉴定审核reducer
 * @Author: Rfan
 * @Date: 2022-07-29 14:24:43
 * @LastEditTime: 2022-07-29 18:53:35
 */

import { delItemByIdArray, editItemById, insertItemAfter } from '@/utils/list';
import {
  SET_SELECTED_ITEM,
  ADD_DISPOSITION_AUDIT_ITEM,
  DELETE_DISPOSITION_AUDIT_ITEM,
  SET_DISPOSITION_AUDIT_LIST,
  EDIT_DISPOSITION_AUDIT_ITEM
} from '../constants';

export const AppraisalAuditReducer = (state, action) => {
  switch (action.type) {
    /**
     * @description: 保存表格选中的条目id
     * @param {number[]} data
     */
    case SET_SELECTED_ITEM:
      return { ...state, selectItems: action.data };

    /**
     * @description: 设置档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case SET_DISPOSITION_AUDIT_LIST: {
      return {
        ...state,
        auditList: action.data
      };
    }

    /**
     * @description: 添加档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case ADD_DISPOSITION_AUDIT_ITEM: {
      return {
        ...state,
        auditList: insertItemAfter(action.data, state.auditList)
      };
    }

    /**
     * @description: 添加档案鉴定批次列表
     * @param {Array} data 导出单列表
     */
    case EDIT_DISPOSITION_AUDIT_ITEM: {
      const { id, data } = action;
      return {
        ...state,
        auditList: editItemById(id, data, state.auditList)
      };
    }

    /**
     * @description: 删除档案鉴定批次
     * @param {number[]} ids 删除的ID列表
     */
    case DELETE_DISPOSITION_AUDIT_ITEM: {
      return {
        ...state,
        auditList: delItemByIdArray(action.ids, state.auditList)
      };
    }

    default:
      console.log('untouched', action);
      return state;
  }
};
